package cn.edu.nhic.order.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.nhic.order.R;
import cn.edu.nhic.order.bean.FoodBean;

/**
 * 店铺详情页适配器
 */
public class ShopDetailAdapter extends BaseAdapter {
    private Context context;  //上下文
    private int resource;     //自定义条目或item的布局
    private List<FoodBean> dataList; //数据源

    public ShopDetailAdapter(Context context, int resource, List<FoodBean> dataList) {
        this.context = context;
        this.resource = resource;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final HoldView hv;
        if(view==null){
            hv=new HoldView();
            LayoutInflater layoutInflater=LayoutInflater.from(this.context);
            view= layoutInflater.inflate(this.resource,null);
            hv.tv_food_name=view.findViewById(R.id.tv_food_name);
            hv.tv_order=view.findViewById(R.id.tv_order);
            hv.tv_price=view.findViewById(R.id.tv_price);
            hv.tv_sale_num=view.findViewById(R.id.tv_sale);
            hv.iv_food_pic=view.findViewById(R.id.iv_food_pic);
            view.setTag(hv);//HoldView 临时存储在视图条目里
        }else{
            hv=(HoldView) view.getTag();
        }
        //给自定义条目组件赋值
        FoodBean fb=dataList.get(i);
        hv.tv_food_name.setText(fb.getFoodName());
        hv.tv_order.setText(fb.getOrder());
        hv.tv_price.setText("￥"+fb.getPrice()+"");
        hv.tv_sale_num.setText(fb.getSaleNum()+fb.getPopularity());
        hv.iv_food_pic.setImageResource(R.drawable.food1);

        return view;
    }

    class HoldView{
        TextView tv_food_name, tv_order,tv_sale_num,tv_price;
        Button btn_add_car;
        ImageView iv_food_pic;
    }
}
